package com.lankystar.logistucs.spring.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lankystar.logistucs.spring.dao.IAddressDao;
import com.lankystar.logistucs.spring.dao.ICargoDao;
import com.lankystar.logistucs.spring.dao.ICompanyDao;
import com.lankystar.logistucs.spring.entity.Address;
import com.lankystar.logistucs.spring.entity.Cargo;
import com.lankystar.logistucs.spring.entity.CargoInfo;
import com.lankystar.logistucs.spring.entity.Company;
import com.lankystar.logistucs.spring.entity.User;
import com.lankystar.logistucs.spring.service.CargoService;

@Service
public class CargoServiceImpl implements CargoService{

	@Autowired
	private ICargoDao cargoDao;
	@Autowired
	private IAddressDao addressDao;
	@Autowired
	private ICompanyDao companyDao;
	@Override
	public void addCargoInfo(CargoInfo record) {
		record.setCreateTime((new Date()).getTime());
		cargoDao.add(record);
	}

	@Override
	public void addCargo(Cargo record,User user,Integer fleetId,Integer brokerId,Integer companyId,Integer addressId) {
		Company fleet = companyDao.selectCompanyById(fleetId);
		Company broker = companyDao.selectCompanyById(brokerId);
		/**
		 * 添加运输公司、报关公司
		 */
		record.setUser(user);
		record.setFleet(fleet);
		record.setBroker(broker);
		record.setRemove(0);
		record.setCreateTime(new Date().getTime());
		/**
		 * 地址写死  （苏州报关行  --> 最低级用户）
		 */
		record.setAddress(addressDao.selectAddressById(1));
		
		cargoDao.add(record);
		/**
		 * 货物详情添加货运单，更新
		 */
		List<CargoInfo> cargoInfolist = cargoDao.selectCargoInfolist(user.getUserId());
		for (CargoInfo info : cargoInfolist) {
			info.setCargo(record);
			cargoDao.update(info);
		}
		/**
		 * 判断是否有最后一公里订单
		 */
		if(companyId!=null){
			Company company = companyDao.selectCompanyById(companyId);
			Cargo cargo = new Cargo();
			cargo.setCount(record.getCount());
			cargo.setCreateTime(new Date().getTime());
			cargo.setHeavy(record.getHeavy());
			cargo.setPointOrderNu(record.getPointOrderNu());
			cargo.setTotalOrderNu(record.getTotalOrderNu());
			cargo.setUploadPdf(record.getUploadPdf());
			cargo.setRemove(0);
			cargo.setUser(user);
			cargoDao.add(cargo);
			/**
			 * 添加运输公司、、、、、没有报关公司
			 */
			cargo.setFleet(company);
			/**
			 * 添加常用地址
			 */
			Address address = addressDao.selectAddressById(addressId);
			cargo.setAddress(address);
			List<CargoInfo> Infolist = cargoDao.selectCargoInfolist(user.getUserId());
			for (CargoInfo cargoInfo : Infolist) {
				cargoInfo.setCargo(cargo);
				cargoDao.update(cargoInfo);
			}
				
		}
		
	}

	@Override
	public List<CargoInfo> queryCargoInfolist(int userId) {
		List<CargoInfo> cargoInfolist = cargoDao.selectCargoInfolist(userId);
		return cargoInfolist;
	}

	@Override
	public List<Map<String, Object>> queryCargolist(int userId) {
		List<Map<String, Object>> maplist = new ArrayList<>();
		List<Object[]> cargolist = cargoDao.selectCargolist(userId);
		for (Object[] objects : cargolist) {
			Cargo cargo = (Cargo) objects[0];
			Address address = (Address) objects[2];
			Map<String, Object> map = new HashMap<>();
			map.put("cargoId", cargo.getCargoId());
			map.put("count", cargo.getCount());
			map.put("heavy", cargo.getHeavy());
			map.put("pointOrderNu", cargo.getPointOrderNu());
			map.put("totalOrderNu", cargo.getTotalOrderNu());
			map.put("uploadPdf", cargo.getUploadPdf());
			map.put("detailed", address.getDetailed());
			maplist.add(map);
		}
		return maplist;
	}

	@Override
	public void updateCargo(Cargo record) {
		cargoDao.update(record);
	}

	@Override
	public void updateCargoInfo(CargoInfo record) {
		cargoDao.update(record);
	}

	@Override
	public void deleteCargoInfo(int infoId) {
		CargoInfo cargoInfo = cargoDao.selectCargoInfoById(infoId);
		cargoDao.delete(cargoInfo);
	}

	@Override
	public void deleteCargo(int cargoId) {
		List<CargoInfo> cargoInfolist = cargoDao.selectCargoInfoBycargoId(cargoId);
		Cargo cargo = cargoDao.selectCargoById(cargoId);
		if(cargoInfolist.isEmpty()){
			cargoDao.delete(cargo);
		}else{
			/**
			 * 级联删除子表
			 */
			for (CargoInfo info : cargoInfolist) {
				cargoDao.delete(info);
			}
			cargoDao.delete(cargo);
		}
		
	}

	@Override
	public Map<String, Object> queryCargoAndAddressBycargoId(int cargoId) {
		Map<String, Object> map = new HashMap<>();
		Object[] ob = cargoDao.selectCargoAndAddressBycargoId(cargoId);
		Cargo cargo = (Cargo) ob[0];
		Address address = (Address) ob[1];
		map.put("totalOrderNu", cargo.getTotalOrderNu());
		map.put("pointOrderNu", cargo.getPointOrderNu());
		map.put("heavy", cargo.getHeavy());
		map.put("detailed", address.getDetailed());
		return map;
	}

	

}
